/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.jm.financeiro.datamodel;

import br.jm.financeiro.entity.LogSistema;
import br.jm.financeiro.entity.LogSistemaDetalhe;
import br.jm.financeiro.sistema.eao.LogSistemaDetalheEao;
import br.jm.financeiro.sistema.eao.LogSistemaEao;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

/**
 *
 * @author joao
 */
public class LazyLogSistema extends LazyDataModel<LogSistema> {

    private static final long serialVersionUID = 3551171342346648590L;

    private List<LogSistema> datasource;
    private final LogSistemaEao logSistemaJpa;
    private final LogSistemaDetalheEao logSistemaDetalheEAO;
    private String cFiltro;
    private String cParametroSQL;
    private boolean lSorter;
    private final int dataSize;

    public LazyLogSistema(LogSistemaEao logSistemaJpa, LogSistemaDetalheEao logSistemaDetalheEAO) {
        this.logSistemaJpa = logSistemaJpa;
        this.logSistemaDetalheEAO = logSistemaDetalheEAO;
        this.dataSize = logSistemaJpa.getCount("LogSistema");
    }

    @Override
    public LogSistema getRowData(String rowKey) {
        for (LogSistema logSistema : datasource) {
            if (logSistema.getDatUpdate().equals(rowKey)) {
                return logSistema;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(LogSistema logSistema) {
        return logSistema.getDatUpdate();
    }

    @Override
    public void setRowIndex(int rowIndex) {
        /*
         * The following is in ancestor (LazyDataModel):
         * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize);
         */
        if (rowIndex == -1 || getPageSize() == 0) {
            super.setRowIndex(-1);
        } else {
            super.setRowIndex(rowIndex % getPageSize());
        }
    }

    @Override
    public List<LogSistema> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        List<LogSistema> data = new ArrayList<>();
        List<LogSistema> dataTemp = new ArrayList<>();
        List<LogSistemaDetalhe> dataDetalhe = new ArrayList<>();
        List<LogSistemaDetalhe> dataDetalheTemp = new ArrayList<>();
        try {
            //filter
            lSorter = SortOrder.ASCENDING.equals(sortOrder);
            data = logSistemaJpa.getNative("LogSistema", first, this.dataSize, "idLog", false, null, cParametroSQL, cFiltro);

            if (filters.containsKey("dscUsuario") || filters.containsKey("dscSistema")) {
                dataDetalhe = logSistemaDetalheEAO.getNative("LogSistemaDetalhe", first, logSistemaDetalheEAO.getCount("LogSistemaDetalhe"), "idLogSistema", false, filters, cParametroSQL, cFiltro);
                if (!dataDetalhe.isEmpty()) {
                    for (LogSistema seletedLogSistema : data) {
                        dataDetalheTemp = new ArrayList<>();
                        for (LogSistemaDetalhe dataDetalhe1 : dataDetalhe) {
                            LogSistema seletedLogSistemaTemp = dataDetalhe1.getIdLog();

                            if (Objects.equals(seletedLogSistema.getIdLog(), seletedLogSistemaTemp.getIdLog())) {
                                dataDetalheTemp.add(dataDetalhe1);
                            }

                        }
                        if (!dataDetalheTemp.isEmpty()) {
                            seletedLogSistema.setLogSistemaDetalheList(dataDetalheTemp);
                            dataTemp.add(seletedLogSistema);
                        }
                    }
                }
                data = dataTemp;
                filters = new HashMap<String, Object>();
            }

        } catch (Exception ex) {
            Logger.getLogger(LazyLogSistema.class.getName()).log(Level.SEVERE, null, ex);
        }

        this.setRowCount(dataSize);

        return data;
    }
}
